package com.czkj.hwz.decorate;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.czkj.hwz.decorate.base.BaseActivity;
import com.czkj.hwz.decorate.base.TabEntity;
import com.czkj.hwz.decorate.fragment.FirstFragment;
import com.czkj.hwz.decorate.fragment.FragmentFactory;
import com.czkj.hwz.decorate.fragment.MeFragment;
import com.czkj.hwz.decorate.fragment.MessageFragment;
import com.czkj.hwz.decorate.fragment.MineFragment;
import com.czkj.hwz.decorate.fragment.MsgFragment;
import com.czkj.hwz.decorate.fragment.ShopFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.activity_frist)
public class FristActivity extends BaseActivity{
    @ViewInject(R.id.fl)
    FrameLayout fl;
    @ViewInject(R.id.tab_index)
    CommonTabLayout tab_index;
    private String[] mTitles = {"首页", "消息", "购物车", "我的"};
    private int[] mIconUnselectIds = {
            R.mipmap.w_first, R.mipmap.w_msg,
            R.mipmap.w_shop, R.mipmap.w_me};
    private int[] mIconSelectIds = {
            R.mipmap.y_frist, R.mipmap.y_msg,
            R.mipmap.y_shop, R.mipmap.y_me};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private long timeMillis;
    FragmentManager fm ;
    private Fragment indexFragment,messageFragment,shopFragment,mineFragment,mContent;
    private FragmentTransaction transaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= 21){
            Window window = getWindow();
            //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            //设置状态栏颜色
            window.setStatusBarColor(getResources().getColor(R.color.yellow));
        }
        for (int i = 0; i <mTitles.length ; i++) {
            mTabEntities.add(new TabEntity(mTitles[i],mIconSelectIds[i],mIconUnselectIds[i]));
        }
        tab_index.setTabData(mTabEntities);
        tab_index.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
//                Fragment fragment= FragmentFactory.createIndexFragment(position);
//                replaceFragment(fragment);

                switch (position) {
                    case 0 :
                        switchContent(indexFragment);
                        break;
                    case 1 :
                        switchContent(messageFragment);
                        break;
                    case 2 :
                        switchContent(shopFragment);
                        break;
                    case 3 :
                        switchContent(mineFragment);
                        break;





                }



            }
            @Override
            public void onTabReselect(int position) {

            }
        });

//        replaceFragment(FristFragment.newInstance(null,null));
//        replaceFragment(new FirstFragment());
        indexFragment=new FirstFragment();
        messageFragment=new MessageFragment();
        shopFragment=new ShopFragment();
        mineFragment=new MineFragment();
        setDefaultFragment(indexFragment);
    }

    private void switchContent(Fragment fragment) {
        if (mContent!=fragment) {
            fm = getSupportFragmentManager();
            transaction=fm.beginTransaction();
            if (fragment.isAdded()) {
                transaction.hide(mContent).add(R.id.fl,fragment).commit();
            }else {
                transaction.hide(mContent).show(fragment).commit();
            }
            mContent=fragment;
        }
    }

    private void setDefaultFragment(Fragment fragment) {
        fm = getSupportFragmentManager();
        transaction=fm.beginTransaction();
        transaction.add(R.id.fl,fragment).commit();
        mContent=fragment;
    }

    private void replaceFragment(Fragment fragment){
        FragmentTransaction fragmentTransaction=fm.beginTransaction();
        fm.beginTransaction().replace(R.id.fl,fragment).commit();
        fm.beginTransaction().add(R.id.fl,fragment);


    }
    @Override
    protected void onResume() {
        super.onResume();
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - timeMillis) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                timeMillis = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
